import random
import re
import json
import time

import requests
from util.ip  import get_proxies
import uuid
# page_uuid 是标准的 UUID v4
page_uuid = str(uuid.uuid4())
timestamp = str(int(time.time() * 1000))
random_digits = ''.join([str(random.randint(0, 9)) for _ in range(20)])
callback = f"jQuery{random_digits}_{timestamp}"
# 发送请求
for i in range(178):
    print(f"正在下载第 {i} 页")
    response = requests.get(
        f'https://re.jd.com/search/getHotSaleGoods?keyword=%E5%AE%B6%E7%94%B5&page={i}&cid3=760&semword=%E5%AE%B6%E7%94%B5&page_uuid={page_uuid}&callback={callback}&_={timestamp}',
        headers={
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
            'cookie': 'shshshfpa=d75d676a-ac80-efda-c145-c60539a2b581-1757057266; shshshfpx=d75d676a-ac80-efda-c145-c60539a2b581-1757057266; __jdu=17570572656551498851836; TrackID=1-PlcuEI-ErI9sh3J4H-al1kmSCVOYVU8KZPOYHJwOBEnsPJhL8wPmPSlQfsQDl57zfuzZXhYR2WBcvbawS0ViZRA0wPP9xzdoxbC3nHEDAI; light_key=AASBKE7rOxgWQziEhC_QY6yasySfPoCiCx9NSyIKsu4rxlh5OdvoDiN8qWvjcOTjxoqrTyBj; pinId=Ywra6hVzuU4lEflXxabr4rV9-x-f3wj7; pin=jd_7aa354ae3f575; unick=jd_d9a5n901dj434k; _tp=U2IKvAWn9hy9CyEKbVc5XdMGQprYMbDRW2%2FPd38UhfU%3D; _pst=jd_7aa354ae3f575; _reuuid=35afd80eee294b3c8a3dba0c92b974c5; thor=81AA8246CA4E618412A77F04A5C24E685B5798CAA47A9C3839FB162C5F84618ACD345C316862E74092EF80669CD887B618641B73B910C108AB1A45E0FA0B8D872EED6D11E12F2D711142385AAC7629286EA9964868BF1F0DACAE83D9F5F591D94B603B9FA8E9709E40BC2AAF384D1C398EA49DC88CDBFB973AAB925F341182A1BCA27975C96A6ED919439214F4F242032C34F0FCEED1EC8006B1774BE20F0A69; joyya=1763710640.1763710745.26.04zgltf; unpl=JF8EALRnNSttCx4AUBJVE0BAHghRW1RYSkcFb2ZSV1UMSgBRHQJJFxZ7XlVdWRRLFx9sZxRUWVNJXA4bAisSEXteU11bD00VB2xXVgQFDQ8WUUtBSUt-S1xTV1wOSRAAa2cGZG1bS2QFGjIbFBRNXFBYXg9PEAdrZwBcVFFPVAwZBSsTIExtZG5aDU0fBWxXBGRcaAkAWRsHHhUXThBUWFkOShMFbGABU1lcS1ENEgsfEhlJWmRfbQs; re_mbp=CAgQ4uHuyQEaLzEwNjgwNzM2MjUxMl8wX2JkZWQ4ZjljYWRkNDQ4YTBhNzEwZjI5ZTBlZTcxYzQ3IABCAnJpWi8xMDY4MDczNjI1MTJfMF9iZGVkOGY5Y2FkZDQ0OGEwYTcxMGYyOWUwZWU3MWM0Nw; areaId=7; ipLoc-djd=7-412-0-0; jsavif=1; areaId=7; mail_times=4%2C1%2C1763724361619; PCSYCityID=CN_410000_410100_0; 3AB9D23F7A4B3C9B=W7PSRUQW2YK4YGLS3M5M5JMWMLDHJEUVJC4GAIR34SQOE2QGAJLQC6LEFN2LROVPGQVBJAZUAROYVS3PJNPHOVFDLA; mt_xid=V2_52007VwMUVltZVlwcTh1bAWMDF1pUUVZaFksebFZvBRNWDl5SRk0dGQkZYgsRUUFQAlJLVRtZDGdRFFMOClRTGnkaXQZjHxJSQVtSSxxIElwMbAIRYl9oUmoYShpaAm4AF1RUXmJfGE8%3D; token=7aec8ae235ec3b7c32eaf4b688df1bc5,3,979848; 3AB9D23F7A4B3CSS=jdd03W7PSRUQW2YK4YGLS3M5M5JMWMLDHJEUVJC4GAIR34SQOE2QGAJLQC6LEFN2LROVPGQVBJAZUAROYVS3PJNPHOVFDLAAAAAM2UZM2DRYAAAAACSNH5LQFANSHGEX; _gia_d=1; cn=46; flash=3_Mrn4Aiohb_Q6lJ6Pivk5Ko9u3O68sAt-V5AJ1rqzAddUjE40a7FvMkGRNNM_GZaI6vp6V09Odbsk2eBNLo7tPAS7uY9uNDOqVj8r2UkQLK8Wmz85QioIkxoOqAu4-4bEKzt9yqvY0Rf4kmibZureJrrhpK6ykLrulP5tHcEH6lnJjr_pxwuTjk**; ipLoc-djd=7-412-416-47178; shshshfpb=BApXWuyZRpf9Aq8l6XscBbnOMr3KCZARbBhVEdUlp9xJ1ItZfQtHXk0m-2Cr_YIAmJODMRnuPsqFbc7E97qhYtIgtN1ri_DWpL0EM; sdtoken=AAbEsBpEIOVjqTAKCQtvQu17R-QqBhmyyVjGrE4BN2wrKh3ZSzi78gW3ZVs9em-TUrKCiPNisZzdo57lfkJSIKClEaTE2CTwK7-XJ_lhLxR9IAqGf-YOADCrt_S9-ykosK9HSOMZ5JibEZx4v4A; __jda=229668127.17570572656551498851836.1757057265.1763720440.1763723819.10; __jdb=229668127.20.17570572656551498851836|10.1763723819; __jdc=229668127; __jdv=229668127|baidu-search|t_262767352_baidusearch|cpc|106807362512_0_bded8f9cadd448a0a710f29e0ee71c47|1763727498064'
        }
        ,
    )
    # print(response.json())
    # 尝试多种正则表达式模式
    patterns = [
        r'jQuery\d+_\d+\((.*)\)',  # 标准 jQuery 格式
        r'^\w+\((.*)\)',           # 任意函数名
        r'\((.*)\)',               # 最宽松的括号匹配
        r'\{.*\}',                 # 直接匹配花括号内容
    ]

    for i, pattern in enumerate(patterns):
        match = re.search(pattern, response.text, re.DOTALL)
        json_str = match.group(1) if i < 3 else match.group(0)
        json_str = json_str.replace('\n', '')
        json_data = json.loads(json_str)
        for item in json_data['list']:
            print(item)
        # print(f"提取的内容: {json_str}")

        break

